﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace HFController
{
    /// <summary>
    /// 法规业务逻辑
    /// </summary>
    public class LegislationController:ControllerBase
    {
        //单列
        private static LegislationController _legislationController;
        //实例化单列
        public  static LegislationController getController()
        {
             if(_legislationController==null)
             {
                _legislationController=new LegislationController();
             }
            return _legislationController;
        }

        #region 法规类

        //法规类数据库访问
        hf.DAL.LegislationClass LegislationClassDAL = new hf.DAL.LegislationClass();

        /// <summary>
        /// 添加法规类
        /// </summary>
        /// <param name="ClassName"></param>
        public void doAddLegislationClass(string ClassName)
        {
            HFModel.ResultModel result = new HFModel.ResultModel();
            hf.Model.LegislationClass model = new hf.Model.LegislationClass();

            model.ClassName = ClassName;
            try
            {
                model.ID = LegislationClassDAL.Add(model);

                result.isTrue = true;
                result.Message = "添加成功！";
                result.goUrl = "location='LegislationClass.aspx';";
                result.historyUrl = "history.go(-1)";
            }
            catch
            {
                result.isTrue = false;
                result.Message = "系统问题，清联系管理员";
                result.goUrl = "history.go(-1)";
                result.historyUrl = "history.go(-1)";
            }
            this.doResult(result);
        }

        /// <summary>
        /// 得到法规类列表，用作绑定下拉框（方法名和下面的一样，这个是重载）
        /// </summary>
        /// <param name="LoadCount">读取记录数</param>
        /// <returns></returns>
        public List<hf.Model.LegislationClass> getLegislationClassList(int LoadCount)
        {
            List<hf.Model.LegislationClass> list = new List<hf.Model.LegislationClass>();
            //总数据记录
            int total;

            //筛选条件
            string strSql = "";

            //获得列表
            list = LegislationClassDAL.GetLegislationClassList(LoadCount, 1, strSql, out total);

            return list;
        }

        /// <summary>
        /// 根据一个ID 得到一条法规类
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public hf.Model.LegislationClass getLegislationClassModel(int ID)
        {
            hf.Model.LegislationClass model = new hf.Model.LegislationClass();
            int total;

            List<hf.Model.LegislationClass> list = LegislationClassDAL.GetLegislationClassList(1, 1, "ID=" + ID.ToString(), out total);
            if (list.Count > 0)
            {
                model = list[0];
            }
            else
            {
                model = null;
            }
            return model;
        }

        /// <summary>
        /// 修改法规类
        /// </summary>
        /// <param name="ID"></param>
        /// <param name="ClassName"></param>
        public void doModifyLegislationClass(int ID, string ClassName)
        {
            HFModel.ResultModel result = new HFModel.ResultModel();
            hf.Model.LegislationClass model = this.getLegislationClassModel(ID);

            model.ClassName = ClassName;
            try
            {
                //执行修改
                LegislationClassDAL.Update(model);

                result.isTrue = true;
                result.Message = "修改成功！";
                result.goUrl = "location='LegislationClass.aspx';";
                result.historyUrl = "history.go(-1)";
            }
            catch
            {
                result.isTrue = false;
                result.Message = "系统问题，清联系管理员";
                result.goUrl = "history.go(-1)";
                result.historyUrl = "history.go(-1)";
            }
            this.doResult(result);
        }

        /// <summary>
        /// 得到法规类类列表
        /// </summary>
        /// <param name="PagerStr"></param>
        /// <returns></returns>
        public List<hf.Model.LegislationClass> getLegislationClassList(out string PagerStr)
        {
            List<hf.Model.LegislationClass> list = new List<hf.Model.LegislationClass>();

            //没有显示记录
            int PageSize = 10;
            //当前页码
            int PageNumber = 1;
            if (HttpContext.Current.Request.QueryString["Page"] != null)
            {
                PageNumber = int.Parse(HttpContext.Current.Request.QueryString["Page"].ToString());
            }
            //记录总条数
            int total;
            //筛选条件
            string strSql = "";

            //获得列表
            list = LegislationClassDAL.GetLegislationClassList(PageSize, PageNumber, strSql, out total);

            //分页
            PagerStr = LLL.Pager.TrunPageHtmlCode(PageSize, PageNumber, total, "");

            return list;
        }

        /// <summary>
        /// 删除法规类
        /// </summary>
        /// <param name="ID"></param>
        public void doDelegeLegislationClass(int ID)
        {
            HFModel.ResultModel result = new HFModel.ResultModel();

            try
            {
                LegislationClassDAL.Delete(ID);

                result.isTrue = true;
                result.Message = "删除成功！";
                result.goUrl = "location='LegislationClass.aspx';";
                result.historyUrl = "history.go(-1)";
            }
            catch
            {
                result.isTrue = false;
                result.Message = "系统问题，清联系管理员";
                result.goUrl = "history.go(-1)";
                result.historyUrl = "history.go(-1)";
            }
            this.doResult(result);
        } 
        #endregion

        #region 法规库

        /// <summary>
        /// 法规库数据库访问
        /// </summary>
        private hf.DAL.LegislationLibary LegislationLibaryDAL = new hf.DAL.LegislationLibary();

        /// <summary>
        /// 添加法规库
        /// </summary>
        /// <param name="LegislationLibaryName"></param>
        public void doAddLegislationLibary(string LegislationLibaryName)
        {
            HFModel.ResultModel result = new HFModel.ResultModel();
            hf.Model.LegislationLibary model = new hf.Model.LegislationLibary();

            model.LegislationLibaryName = LegislationLibaryName;

            try
            {
                model.ID = LegislationLibaryDAL.Add(model);

                result.isTrue = true;
                result.Message = "添加成功！";
                result.goUrl = "location='LegislationLibary.aspx';";
                result.historyUrl = "history.go(-1)";
            }
            catch
            {
                result.isTrue = false;
                result.Message = "系统问题，清联系管理员";
                result.goUrl = "history.go(-1)";
                result.historyUrl = "history.go(-1)";
            }
            this.doResult(result);
        }

        /// <summary>
        /// 得到法规库列表，用作绑定下拉框（方法名和下面的一样，这个是重载）
        /// </summary>
        /// <param name="LoadCount">读取记录数</param>
        /// <returns></returns>
        public List<hf.Model.LegislationLibary> getLegislationLibaryList(int LoadCount)
        {
            List<hf.Model.LegislationLibary> list = new List<hf.Model.LegislationLibary>();
            //总数据记录
            int total;

            //筛选条件
            string strSql = "";

            //获得列表
            list = LegislationLibaryDAL.GetLegislationLibaryList(LoadCount, 1, strSql, out total);

            return list;
        }

        /// <summary>
        /// 根据一个ID 得到一条法规库
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public hf.Model.LegislationLibary getLegislationLibaryModel(int ID)
        {
            hf.Model.LegislationLibary model = new hf.Model.LegislationLibary();
            int total;

            List<hf.Model.LegislationLibary> list = LegislationLibaryDAL.GetLegislationLibaryList(1, 1, "ID=" + ID.ToString(), out total);
            if (list.Count > 0)
            {
                model = list[0];
            }
            else
            {
                model = null;
            }
            return model;
        }

        /// <summary>
        /// 修改法规库
        /// </summary>
        /// <param name="ID"></param>
        /// <param name="LegislationLibaryName"></param>
        public void doModifyLegislationLibary(int ID, string LegislationLibaryName)
        {
            HFModel.ResultModel result = new HFModel.ResultModel();
            hf.Model.LegislationLibary model = this.getLegislationLibaryModel(ID);

            model.LegislationLibaryName = LegislationLibaryName;

            try
            {
                //执行修改
                LegislationLibaryDAL.Update(model);

                result.isTrue = true;
                result.Message = "修改成功！";
                result.goUrl = "location='LegislationLibary.aspx';";
                result.historyUrl = "history.go(-1)";
            }
            catch
            {
                result.isTrue = false;
                result.Message = "系统问题，清联系管理员";
                result.goUrl = "history.go(-1)";
                result.historyUrl = "history.go(-1)";
            }
            this.doResult(result);
        }

        /// <summary>
        /// 得到法规库列表
        /// </summary>
        /// <param name="PagerStr"></param>
        /// <returns></returns>
        public List<hf.Model.LegislationLibary> getLegislationLibaryList(out string PagerStr)
        {
            List<hf.Model.LegislationLibary> list = new List<hf.Model.LegislationLibary>();

            //没有显示记录
            int PageSize = 10;
            //当前页码
            int PageNumber = 1;
            if (HttpContext.Current.Request.QueryString["Page"] != null)
            {
                PageNumber = int.Parse(HttpContext.Current.Request.QueryString["Page"].ToString());
            }
            //记录总条数
            int total;
            //筛选条件
            string strSql = "";

            //获得列表
            list = LegislationLibaryDAL.GetLegislationLibaryList(PageSize, PageNumber, strSql, out total);

            //分页
            PagerStr = LLL.Pager.TrunPageHtmlCode(PageSize, PageNumber, total, "");

            return list;
        }

        /// <summary>
        /// 删除法规库
        /// </summary>
        /// <param name="ID"></param>
        public void doDelegeLegislationLibary(int ID)
        {
            HFModel.ResultModel result = new HFModel.ResultModel();
            try
            {
                LegislationLibaryDAL.Delete(ID);

                result.isTrue = true;
                result.Message = "删除成功！";
                result.goUrl = "location='LegislationLibary.aspx';";
                result.historyUrl = "history.go(-1)";
            }
            catch
            {
                result.isTrue = false;
                result.Message = "系统问题，清联系管理员";
                result.goUrl = "history.go(-1)";
                result.historyUrl = "history.go(-1)";
            }
            this.doResult(result);
        }
        #endregion

        #region 法规

        /// <summary>
        /// 法规数据库访问
        /// </summary>
        private hf.DAL.Legislation LegislationDAL = new hf.DAL.Legislation();

        /// <summary>
        /// 添加法规
        /// </summary>
        /// <param name="LegislationLibaryID"></param>
        /// <param name="LegislationClassID"></param>
        /// <param name="ProvinceID"></param>
        /// <param name="CityID"></param>
        /// <param name="StatusID"></param>
        /// <param name="Title"></param>
        /// <param name="Addtime"></param>
        /// <param name="Effectivetime"></param>
        /// <param name="Content"></param>
        public void doAddLegislation(string LegislationLibaryID, string LegislationClassID, int ProvinceID, int CityID, int StatusID, string Title,DateTime Effectivetime, string Content)
        {
            HFModel.ResultModel result = new HFModel.ResultModel();
            hf.Model.Legislation model = new hf.Model.Legislation();

            model.LegislationLibaryID = int.Parse(LegislationLibaryID);
            model.LegislationClassID = int.Parse(LegislationClassID);
            model.ProvinceID = ProvinceID;
            model.CityID = CityID;
            model.StatusID = StatusID;
            model.Title = Title;
            model.Addtime = DateTime.Now;
            model.Effectivetime = Effectivetime;
            model.Content = Content;
            try 
            {
                model.ID = LegislationDAL.Add(model);

                result.isTrue = true;
                result.Message = "添加成功！";
                result.goUrl = "location='Legislation.aspx';";
                result.historyUrl = "history.go(-1)";
            }
            catch 
            {
                result.isTrue = false;
                result.Message = "系统问题，清联系管理员";
                result.goUrl = "history.go(-1)";
                result.historyUrl = "history.go(-1)";
            }
            this.doResult(result);
        }

        /// <summary>
        /// 根据ID 得到一条法规列
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public hf.Model.Legislation getLegislationModel(int ID)
        {
            hf.Model.Legislation model = new hf.Model.Legislation();
            int total;

            List<hf.Model.Legislation> list = LegislationDAL.GetLegislationList(1, 1, "ID=" + ID.ToString(), out total);
            if (list.Count > 0)
            {
                model = list[0];
            }
            else
            {
                model = null;
            }
            return model;
        }

        /// <summary>
        /// 修改法规
        /// </summary>
        /// <param name="ID"></param>
        /// <param name="LegislationLibaryID"></param>
        /// <param name="LegislationClassID"></param>
        /// <param name="ProvinceID"></param>
        /// <param name="CityID"></param>
        /// <param name="StatusID"></param>
        /// <param name="Title"></param>
        /// <param name="Effectivetime"></param>
        /// <param name="Content"></param>
        public void doModifyLegislation(int ID, string LegislationLibaryID, string LegislationClassID, int ProvinceID, int CityID, int StatusID, string Title, DateTime Effectivetime, string Content)
        {
            HFModel.ResultModel result = new HFModel.ResultModel();
            hf.Model.Legislation model = this.getLegislationModel(ID);

            model.LegislationLibaryID = int.Parse(LegislationLibaryID);
            model.LegislationClassID = int.Parse(LegislationClassID);
            model.ProvinceID = ProvinceID;
            model.CityID = CityID;
            model.StatusID = StatusID;
            model.Title = Title;
            model.Effectivetime = Effectivetime;
            model.Content = Content;

            try
            {
                LegislationDAL.Update(model);
                result.isTrue = true;
                result.Message = "修改成功！";
                result.goUrl = "location='LegislationList.aspx';";
                result.historyUrl = "history.go(-1)";
            }
            catch
            {
                result.isTrue = false;
                result.Message = "系统问题，清联系管理员";
                result.goUrl = "history.go(-1)";
                result.historyUrl = "history.go(-1)";
            }
            this.doResult(result);
        }

        /// <summary>
        /// 前台得到法规
        /// </summary>
        /// <param name="PageSize"></param>
        /// <param name="PageNumber"></param>
        /// <param name="strWhere"></param>
        /// <param name="total"></param>
        /// <returns></returns>
        public List<hf.Model.Legislation> getLegislationList(int PageSize, int PageNumber, string strWhere, out int total)
        {
            List<hf.Model.Legislation> list = new List<hf.Model.Legislation>();
            //获得列表
            list = LegislationDAL.GetLegislationList(PageSize, PageNumber, strWhere, out total);
            this.BindParameter(list);
            return list;
        }

        /// <summary>
        /// 得到指定数量的法规
        /// </summary>
        /// <param name="Count"></param>
        /// <returns></returns>
        public List<hf.Model.Legislation> getLegislationList(int Count)
        {
            List<hf.Model.Legislation> list = new List<hf.Model.Legislation>();

            //每页显示的记录
            int PageSize = Count;

            //总数据记录
            int total;

            //筛选条件
            string strSql = "";

            //获得列表
            list = LegislationDAL.GetLegislationList(PageSize, 1, strSql, out total);
            this.BindParameter(list);
            return list;
        }
        /// <summary>
        /// 得到法规表
        /// </summary>
        /// <param name="PagerStr"></param>
        /// <returns></returns>
        public List<hf.Model.Legislation> getLegislationList(out string PagerStr)
        {
            List<hf.Model.Legislation> list = new List<hf.Model.Legislation>();

            //每页显示的记录
            int PageSize = 10;
            //当前页码
            int PageNumber = 1;
            if (HttpContext.Current.Request.QueryString["Page"] != null)
            {
                PageNumber = int.Parse(HttpContext.Current.Request.QueryString["Page"].ToString());
            }

            //总数据记录
            int total;

            //筛选条件
            string strSql = "";

            //获得列表
            list = LegislationDAL.GetLegislationList(PageSize, PageNumber, strSql, out total);

            //分页
            PagerStr = LLL.Pager.TrunPageHtmlCode(PageSize, PageNumber, total, "");

            return list;
        }

        private void BindParameter(List<hf.Model.Legislation> list)
        {
            foreach (hf.Model.Legislation l in list)
            {
                l.LegislationLibaryName = LegislationController.getController().getLegislationLibaryModel(l.LegislationLibaryID).LegislationLibaryName;
            }
        }

        /// <summary>
        /// 删除法规
        /// </summary>
        /// <param name="ID"></param>
        public void doDelegeLegislation(int ID)
        {
            HFModel.ResultModel result = new HFModel.ResultModel();
            try
            {
                LegislationDAL.Delete(ID);

                result.isTrue = true;
                result.Message = "删除成功！";
                result.goUrl = "location='LegislationList.aspx';";
                result.historyUrl = "history.go(-1)";
            }
            catch
            {
                result.isTrue = false;
                result.Message = "系统问题，清联系管理员";
                result.goUrl = "history.go(-1)";
                result.historyUrl = "history.go(-1)";
            }
            this.doResult(result);
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="IDS"></param>
        public void doDelegeLegislation(string[] IDS)
        {
            HFModel.ResultModel result = new HFModel.ResultModel();
            try
            {
                for (int i = 0; i < IDS.Length; i++)
                {
                    LegislationDAL.Delete(int.Parse(IDS[i].Trim()));
                }
                result.isTrue = true;
                result.Message = "删除成功！";
                result.goUrl = "location='LegislationList.aspx';";
                result.historyUrl = "history.go(-1)";
            }
            catch
            {
                result.isTrue = false;
                result.Message = "系统问题，清联系管理员";
                result.goUrl = "history.go(-1)";
                result.historyUrl = "history.go(-1)";
            }
            this.doResult(result);
        }
        #endregion
    }
}
